package com.csu.express.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csu.common.exception.WebException;
import com.csu.common.utils.UUIDUtil;
import com.csu.express.dao.OrderMapper;
import com.csu.express.enums.OrderStatus;
import com.csu.express.enums.QueryStatus;
import com.csu.express.pojo.OrderPojo;
import com.csu.express.service.CustomerService;
import com.csu.express.service.DriverService;
import com.csu.express.service.OrderService;
import com.csu.express.vo.AssessVo;
import com.csu.express.vo.CustomerVo;
import com.csu.express.vo.DriverVo;
import com.csu.express.vo.OrderDriverVo;
import com.csu.express.vo.OrderRequestVo;
import com.csu.express.vo.OrderResponseVo;
import com.csu.express.vo.PositionVo;

/**
 * @项目名称：express
 * @类名称：OrderServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年10月23日 下午6:12:11
 * @company:
 */
@Service
public class OrderServiceImpl implements OrderService
{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DriverService driverService;

    @Autowired
    private CustomerService customerService;

    /**
     * @see com.csu.express.service.OrderService#assessOrder(com.csu.express.vo.AssessVo)
     */
    @Override
    public Double assessOrder(AssessVo assessVo)
    {
        // TODO Auto-generated method stub

        double matrixPrice = assess(assessVo.getSourcePosition(), assessVo.getTargetPosition());

        return matrixPrice * assessVo.getRequiredCapacity();
    }

    /**
     * @see com.csu.express.service.OrderService#addOrder(com.csu.express.vo.OrderRequestVo)
     */
    @Override
    public OrderPojo addOrder(OrderRequestVo orderRequest)
    {
        OrderPojo order = new OrderPojo();
        order.setCustomerId(orderRequest.getCustomerId());
        order.setLinkman(orderRequest.getLinkman());
        order.setLinknumber(orderRequest.getLinknumber());
        order.setApplyTime(new Date());
        order.setRequiredCapacity(orderRequest.getRequiredCapacity());
        order.setStatus(OrderStatus.Apply.getValue());
        order.setSerialNumber(UUIDUtil.getUUID());
        order.setServiceType(orderRequest.getServiceType());
        order.setMatrixPrice(assess(orderRequest.getSourcePosition(), orderRequest.getTargetPosition()));
        order.setSourceAddress(orderRequest.getSourcePosition().getDetailAddress());
        order.setTargetAddress(orderRequest.getTargetPosition().getDetailAddress());
        orderMapper.insert(order);
        return order;
    }

    /**
     * @description 估价
     * @author TangC
     * @date 2017年10月28日 下午11:42:27
     * @param sourcePosition
     * @param targetPosition
     * @return
     */
    private Double assess(PositionVo sourcePosition, PositionVo targetPosition)
    {
        // TODO Auto-generated method stub
        return 123.45;
    }

    /**
     * @throws WebException
     * @see com.csu.express.service.OrderService#getOrderInfoBySn(java.lang.String)
     */
    @Override
    public OrderResponseVo getOrderInfoBySn(String serialNumber) throws WebException
    {
        OrderResponseVo orderRep = new OrderResponseVo();

        OrderPojo order = orderMapper.selectBySn(serialNumber);
        DriverVo driverVo = driverService.getDriverInfoBySn(order.getDriverSn());

        orderRep.setDriver(driverVo);
        orderRep.setOrder(order);
        return orderRep;

    }

    /**
     * @see com.csu.express.service.OrderService#getOrderListByCustomerId(int,
     *      int, int)
     */
    @Override
    public List<OrderResponseVo> getOrderListByCustomerId(int customerId, int begin, int size, Integer queryStatus)
            throws WebException
    {

        List<OrderResponseVo> orderReps = new ArrayList<OrderResponseVo>();
        List<Integer> statusList = getStatusByQueryStatus(queryStatus);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("customerId", customerId);
        params.put("begin", begin);
        params.put("size", size);
        params.put("statusList", statusList);
        List<OrderPojo> orders = orderMapper.selectOrdersByCustomerIdAndStatus(params);

        List<String> driverSns = new ArrayList<String>();
        for (OrderPojo order : orders)
        {
            String driverSn = order.getDriverSn();
            driverSns.add(driverSn);
        }
        List<DriverVo> driverVos = driverService.getDriverInfosBySns(driverSns);

        Map<String, DriverVo> driverMap = new HashMap<String, DriverVo>();
        for (DriverVo driverVo : driverVos)
        {
            driverMap.put(driverVo.getDriverSn(), driverVo);
        }

        for (OrderPojo order : orders)
        {
            OrderResponseVo orderResponseVo = new OrderResponseVo();

            String driverSn = order.getDriverSn();
            DriverVo driverVo = driverMap.get(driverSn);
            orderResponseVo.setDriver(driverVo);
            orderResponseVo.setOrder(order);
            
            orderReps.add(orderResponseVo);
        }
        return orderReps;
    }

    /**
     * @description
     * @author TangC
     * @date 2017年11月2日 下午10:43:35
     * @param queryStatus
     * @return
     */
    private List<Integer> getStatusByQueryStatus(Integer queryStatus)
    {
        List<Integer> statusList = new ArrayList<Integer>();
        if (QueryStatus.All.getValue() == queryStatus)
        {
            statusList.addAll(OrderStatus.getAllStatus());
        }
        else if (QueryStatus.Pickup.getValue() == queryStatus)
        {
            statusList.add(OrderStatus.Accept.getValue());
            statusList.add(OrderStatus.Arrive.getValue());
        }
        else if (QueryStatus.Deliver.getValue() == queryStatus)
        {
            statusList.add(OrderStatus.Loading.getValue());
            statusList.add(OrderStatus.ToBack.getValue());
        }

        else if (QueryStatus.Finish.getValue() == queryStatus)
        {
            statusList.add(OrderStatus.Recive.getValue());
            statusList.add(OrderStatus.Backed.getValue());
            statusList.add(OrderStatus.Expire.getValue());
            statusList.add(OrderStatus.Cancel.getValue());
        }

        return statusList;
    }

    /**
     * @see com.csu.express.service.OrderService#cancelOrder(java.lang.String,
     *      java.lang.Integer)
     */
    @Override
    public Integer cancelOrder(String serialNumber, Integer customerId)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("serialNumber", serialNumber);
        params.put("customerId", customerId);
        params.put("status", OrderStatus.Cancel.getValue());
        return orderMapper.updateStatusBySNAndCId(params);
    }

    @Override
    public List<OrderDriverVo> getOrderListByDriverId(int driverId, int begin, int size, Integer queryStatus) throws WebException
    {
        List<OrderDriverVo> orderDriverVos = new ArrayList<>();
        List<Integer> statusList = getStatusByQueryStatus(queryStatus);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("driverId", driverId);
        params.put("begin", begin);
        params.put("size", size);
        params.put("statusList", statusList);
        List<OrderPojo> orders = orderMapper.selectOrdersByDriverIdAndStatus(params);

        for (OrderPojo order : orders)
        {
            OrderDriverVo orderDriverVo = new OrderDriverVo();
            CustomerVo customerVo = customerService.getCustomerInfoById(order.getCustomerId());
            orderDriverVo.setCustomerVo(customerVo);
            orderDriverVo.setOrder(order);
            orderDriverVos.add(orderDriverVo);
        }
        return orderDriverVos;
    }
}
